(function (vc) {

    vc.extends({
        propTypes: {
            callBackListener: vc.propTypes.string, //父组件名称
            callBackFunction: vc.propTypes.string //父组件监听方法
        },
        data: {
            addShopSettingInfo: {
                shopId: vc.getCurrentCommunity().shopId,
                settingName: '',
                settingType: '',
                settingKey: '',
                keys: [],
                settingValue: '',
                remark: '',
            }
        },
        _initMethod: function () {

        },
        _initEvent: function () {
            vc.on('addShopSetting', 'openAddShopSettingModal', function () {
                $('#addShopSettingModel').modal('show');
            });
        },
        methods: {
            addShopSettingValidate() {
                return vc.validate.validate({
                    addShopSettingInfo: $that.addShopSettingInfo
                }, {
                    'addShopSettingInfo.shopId': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "店铺ID不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "30",
                            errInfo: "店铺ID不能超过30"
                        },
                    ],
                    'addShopSettingInfo.settingName': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "名称不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "64",
                            errInfo: "名称不能超过64"
                        },
                    ],
                    'addShopSettingInfo.settingType': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "设置类型不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "30",
                            errInfo: "设置类型不能超过30"
                        },
                    ],
                    'addShopSettingInfo.settingKey': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "设置key不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "30",
                            errInfo: "设置key不能超过30"
                        },
                    ],
                    'addShopSettingInfo.settingValue': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "设置值不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "255",
                            errInfo: "设置值不能超过255"
                        },
                    ]
                });
            },
            saveShopSettingInfo: function () {
                if (!$that.addShopSettingValidate()) {
                    vc.toast(vc.validate.errInfo);

                    return;
                }
                //不提交数据将数据 回调给侦听处理
                if (vc.notNull($props.callBackListener)) {
                    vc.emit($props.callBackListener, $props.callBackFunction, $that.addShopSettingInfo);
                    $('#addShopSettingModel').modal('hide');
                    return;
                }
                vc.http.apiPost(
                    '/shopSetting/saveShopSetting',
                    JSON.stringify($that.addShopSettingInfo),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            //关闭model
                            $('#addShopSettingModel').modal('hide');
                            $that.clearAddShopSettingInfo();
                            vc.emit('shopSettingManage', 'listShopSetting', {});

                            return;
                        }
                        vc.toast(_json.msg);

                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    });
            },
            _changeSettingType: function() {
                $that._loadShopSettingKey();
            },
            _changeSettingName: function() {
                $that.addShopSettingInfo.keys.forEach(item => {
                    if (item.settingName == $that.addShopSettingInfo.settingName) {
                        $that.addShopSettingInfo.settingKey = item.settingKey;
                        $that.addShopSettingInfo.remark = item.remark;
                    }
                });

            },
            _loadShopSettingKey: function() {
                var param = {
                    params: {
                        page:1,
                        row:50,
                        shopId: vc.getCurrentCommunity().shopId,
                        settingType: $that.addShopSettingInfo.settingType
                    }
                };
                //发送get请求
                vc.http.apiGet('/shopSettingKey/queryShopSettingKey',
                    param,
                    function(json, res) {
                        let _communitySettingManageInfo = JSON.parse(json);
                        $that.addShopSettingInfo.keys = _communitySettingManageInfo.data;
                        $that.addShopSettingInfo.settingKey = "必填，请选择配置KEY";
                    },
                    function(errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            clearAddShopSettingInfo: function () {
                $that.addShopSettingInfo = {
                    shopId: '',
                    settingName: '',
                    keys: [],
                    settingType: '',
                    settingKey: '',
                    settingValue: '',
                    remark: '',
                };
            }
        }
    });

})(window.vc);
